using System;
using System.Collections.Generic;
using System.Text;
using SP.Parameter;

namespace SP.HwDeferredRS
{
    /// <summary>
    /// A dirt map feature
    /// </summary>
    public class DirtMapFusion : AbstractFusion
    {
        public DirtMapFusion()
        {
            AddPreprocessor(new Preprocessor("DIRTMAP_UV", 1, 3, 1, "UV channel (1-3) for dirtMap."));
            AddPreprocessor(new Preprocessor("COMPOSITEMAP_UV", 1, 3, 1, "UV channel (1-3) for compositeMap."));

            // param dirtMap
            AddParameter(
                new Texture2dParamter("dirtMap", "dirtMapSamp")
                .SetSRGBTexture(true)
                .SetUserData("fragment")
                );

            // param compositeMap
            AddParameter(
                new Texture2dParamter("compositeMap", "compositeMapSamp")
                .SetSRGBTexture(false)
                .SetUserData("fragment")
                );
        }

        public override string Name
        {
            get { return "DirtMap"; }
        }

        public override string Desc
        {
            get { return ""; }
        }

        public override Stage Stage
        {
            get { return Stage.AlbedoGloss; }
        }

        public override int UVChannelCount
        {
            get
            {
                int dirtUV = GetPreprocessor("DIRTMAP_UV").Value;
                int compUV = GetPreprocessor("COMPOSITEMAP_UV").Value;
                return Math.Max(dirtUV, compUV);
            }
        }

        public override void Code(StringBuilder b, string platform)
        {
            AddLine(b, "vec4 dirtMap = tex2D(dirtMapSamp, pipeline.Texcoord[DIRTMAP_UV-1] );");
            AddLine(b, "float blendWeight = tex2D(compositeMapSamp, pipeline.Texcoord[COMPOSITEMAP_UV-1]).r;");
            AddLine(b, "blendWeight *= tex2D(compositeMapSamp, pipeline.Texcoord[DIRTMAP_UV-1]).a;");
            AddLine(b, "pipeline.Albedo = lerp(pipeline.Albedo, dirtMap.rgb, blendWeight);");
            AddLine(b, "pipeline.Gloss = lerp(pipeline.Gloss, dirtMap.a, blendWeight);");
        }
    }
}